﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Popup.Master" AutoEventWireup="true"
    CodeBehind="Import.aspx.cs" Inherits="PromotionEngine.Import" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <style type="text/css">
        .k-upload-selected
        {
            display: none;
        }
    </style>
    <style type="text/css">
        .file-icon
        {
            display: inline-block;
            float: left;
            width: 48px;
            height: 48px;
            margin-left: 10px;
            margin-top: 13.5px;
        }
        
        .img-file
        {
            background-image: url(../content/web/upload/jpg.png);
        }
        .doc-file
        {
            background-image: url(../content/web/upload/doc.png);
        }
        .pdf-file
        {
            background-image: url(../content/web/upload/pdf.png);
        }
        .xls-file
        {
            background-image: url(../content/web/upload/xls.png);
        }
        .zip-file
        {
            background-image: url(../content/web/upload/zip.png);
        }
        .default-file
        {
            background-image: url(../content/web/upload/default.png);
        }
        
        #example .file-heading
        {
            font-family: Arial;
            font-size: 1.1em;
            display: inline-block;
            float: left;
            width: 450px;
            margin: 0 0 0 20px;
            height: 25px;
            -ms-text-overflow: ellipsis;
            -o-text-overflow: ellipsis;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
        }
        
        #example .file-name-heading
        {
            font-weight: bold;
        }
        
        #example .file-size-heading
        {
            font-weight: normal;
            font-style: italic;
        }
        
        li.k-file .file-wrapper .k-upload-action
        {
            position: absolute;
            top: 0;
            right: 0;
        }
        
        li.k-file div.file-wrapper
        {
            position: relative;
            height: 75px;
        }
    </style>
    <script type="text/javascript">
        var fileUpload;
        function DownloadTemplate() {
            $('#errMsg').text('');
            var dataType = $("#DataType").val();
            if (dataType == '') {
                $('#errMsg').text("Please select Target.");
                $('#errMsg').css('color', "red");
                return;
            }
            else {
                $.ajax({
                    type: "POST",
                    url: "Import.aspx/GetTemplate",
                    data: JSON.stringify({
                        dataType: dataType
                    }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var file = data.d.replace('\"', '').replace('\"', '');
                        setTimeout(function () { window.location.href = file; }, 1000);
                    },
                    error: function (xhr, status, err) {
                        alert(xhr.responseText);
                        alert(err.Message);
                    }
                });
            }
        }
        var onSelectUploadDataFile = function (event) {
            $('#errMsg').text('');
            $.each(event.files, function (index, value) {
                if (value.extension !== '.csv') {
                    event.preventDefault();
                    $('#errMsg').text("Please select csv file.");
                    $('#errMsg').css('color', "red");
                }
                //console.log("Name: " + value.name);
                //console.log("Size: " + value.size + " bytes");
                //console.log("Extension: " + value.extension);
            });
        };
        function closeImport() {
            resetUpload();
            parent.$("#importPopup").data("kendoWindow").close();
        }
        function ImportProcess(e) {
            $('#errMsg').text('');
            var dataType = $("#DataType").val();
            if (dataType == '') {
                $('#errMsg').text("Please select Target.");
                $('#errMsg').css('color', "red");
                return;
            }
            else {
                $('.k-upload-selected').trigger('click');
            }
        }
        function getFileInfo(e) {
            return $.map(e.files, function (file) {
                var info = file.name;
                // File size is not available in all browsers
                if (file.size > 0) {
                    info += " (" + Math.ceil(file.size / 1024) + " KB)";
                }
                return info;
            }).join(", ");
        }
        function onUpload(e) {
            // var upload = e.files;
            // //get the options and add to the Url
            // var postUrl = "Upload.aspx";
            // this.options.async.saveUrl = postUrl;
        }

        function onSuccess(e) {
            $('#errMsg').text('Importing.....');
            $('#errMsg').css('color', "blue");
            var dataType = $("#DataType").val();
            $.ajax({
                type: "POST",
                url: "Import.aspx/Save",
                data: JSON.stringify({
                    dataType: dataType
                }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (data.d != '') {
                        $('#errMsg').text(data.d);
                        $('#errMsg').css('color', "red");
                    }
                    else {
                        $('#errMsg').text("Operation complete successful!");
                        $('#errMsg').css('color', "blue");
                    }
                },
                error: function (xhr, status, err) {
                    alert(xhr.responseText);
                    alert(err.Message);
                }
            });
        }
        function onComplete(e) {

        }
        function onError(e) {
            alert(e.XMLHttpRequest.responseText);
        }
        $(document).ready(function () {
            var data = [
                        { text: "Customer", value: "Customer" },
                        { text: "Item", value: "Item" },
                        { text: "Warehouse", value: "Warehouse" },
                        { text: "Sales Person", value: "SalesPerson" }
                    ];

            $("#DataType").kendoDropDownList({
                autoBind: false,
                //filter: "contains",
                optionLabel: "[Select Target]",
                dataTextField: "text",
                dataValueField: "value",
                //suggest: true,
                required: true,
                dataSource: data
            });
            fileUpload = $("#files").kendoUpload({
                multiple: false,
                showFileList: true,
                async: {
                    saveUrl: "Upload.aspx",
                    autoUpload: false
                },
                localization: {
                    select: 'Select csv file'
                },
                complete: onComplete,
                select: onSelectUploadDataFile,
                success: onSuccess,
                error: onError,
                upload: onUpload
            });
        });
        function addExtensionClass(extension) {
            switch (extension) {
                case '.jpg':
                case '.img':
                case '.png':
                case '.gif':
                    return "img-file";
                case '.doc':
                case '.docx':
                    return "doc-file";
                case '.xls':
                case '.xlsx':
                    return "xls-file";
                case '.pdf':
                    return "pdf-file";
                case '.zip':
                case '.rar':
                    return "zip-file";
                default:
                    return "default-file";
            }
        }
    </script>
    <script id="fileTemplate" type="text/x-kendo-template">
                <span class='k-progress'></span>
                <div class='file-wrapper'>
                    <span class='file-icon #=addExtensionClass(files[0].extension)#'></span>
                    <h4 class='file-heading file-name-heading'>Name: #=name#</h4>
                    <h4 class='file-heading file-size-heading'>Size: #=size# bytes</h4>
                    <button type='button' class='k-upload-action'></button>
                </div>
            </script>
    <table style="width: 100%; height: 100%;" border="0px">
        <tr>
            <td>
                Target
            </td>
            <td align="left" style="width: 55%;">
                <input type="text" name="DataType" id="DataType" style="margin-left: 13px; width: 280px;" />
            </td>
            <td align="right">
                <button type="button" id="Button1" onclick="DownloadTemplate();" class="k-button k-button-icontext k-primary"
                    style="margin-right: 10px;">
                    <span class="k-icon k-i-arrow-s"></span>Get Import Template</button>
            </td>
        </tr>
        <tr>
            <td>
                File
            </td>
            <td style="height: 40%;" align="left" colspan="2">
                <input name="files" id="files" type="file" runat="server" clientidmode="Static" />
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <div class="k-edit-form-container" style="width: 100%;">
                    <div class="k-edit-buttons k-state-default">
                        <button type="button" id="btnImport" onclick="ImportProcess();" class="k-button k-button-icontext k-primary">
                            <span class="k-icon k-update"></span>Import</button><a class="k-button k-button-icontext k-grid-cancel"
                                href="#" id="btnClose" onclick="closeImport();"><span class="k-icon k-cancel"></span>Close</a></div>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <span id="errMsg"></span>
            </td>
        </tr>
    </table>
</asp:Content>
